/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.lsi.sgb.dao;

import br.com.lsi.sgb.util.Endereco;
import br.com.lsi.sgb.util.HibernateUtil;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;

/**
 *
 * @author DiegoNicacio
 */
public class EnderecoDao {

    Session sessao = null;

    public void addEndereco(Endereco endereco) {
        sessao = HibernateUtil.getSessionFactory().openSession();
        sessao.beginTransaction();
        sessao.save(endereco);
        sessao.getTransaction().commit();
        sessao.close();
    }

    public List listar() {
        sessao = HibernateUtil.getSessionFactory().openSession();
        sessao.beginTransaction();
        List<Endereco> lista = new ArrayList<Endereco>();
        lista = sessao.createQuery("from Pessoa where DTYPE = 'Endereco'").list();
        sessao.getTransaction().commit();
        sessao.close();
        return lista;
    }

    public Endereco buscaEndereco(Long id) {
        sessao = HibernateUtil.getSessionFactory().openSession();
        sessao.beginTransaction();
        Endereco e = (Endereco) sessao.get(Endereco.class, id);
        sessao.getTransaction().commit();
        sessao.close();
        return e;
    }

    public boolean removeEndereco(Endereco endereco) {
        sessao = HibernateUtil.getSessionFactory().openSession();
        sessao.beginTransaction();
        sessao.delete(endereco);
        sessao.getTransaction().commit();
        sessao.close();
        return true;
    }

    public void atualizarEndereco(Endereco endereco) {
        sessao = HibernateUtil.getSessionFactory().openSession();
        sessao.beginTransaction();
        sessao.update(endereco);
        sessao.getTransaction().commit();
        sessao.close();
    }
}
